﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Openfree.Framework.DataAccess;
using ICSS.Office.Word;
using ICSS.Office.Core;
using GenCode128;
using ICSS.Office.Excel;
using System.Collections.Specialized;
using System.Xml;


/// <summary>
/// Summary description for PMSAccess
/// </summary>
public class PMSAccess : BaseAccess
{

    public const string AppendSql_UserOrg = " ,(select UserName from T_User where UserID=T.LoginName) as UserName,(select Subject from T_OrgTree where OrgID=T.Department) as OrgName ";
    public const string AppendSql_User = " ,(select UserName from T_User where UserID=T.LoginName) as UserName ";
    public const string AppendSql_Org = " ,(select Subject from T_OrgTree where OrgID=T.Department) as OrgName ";
    public PMSAccess()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public static DataSet GetDataSet(string tableName,string key,string value)
    {
        return BaseAccess.GetDataSet(ConfigManager.ConnectionString,string.Format("select * from {0} where {1} ='{2}'",tableName,key,value),tableName);
    }

    public static DataSet GetEmptyDataSet(string tableName)
    {
        return BaseAccess.GetDataSet(ConfigManager.ConnectionString, string.Format("select * from {0} where 1=2", tableName), tableName);
    }
    public static void UpdateDataSet(string tableName, DataSet ds)
    {
        BaseAccess.UpdateDataSet(ConfigManager.ConnectionString,tableName,ds);
    } 
    public static void AppendValue(ref DataRow dr,EnumOperator kind)
    {
        switch (kind)
        {
            case EnumOperator.ADD:
                dr["CreateTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                dr["CreateUser"] = SessionManager.UserID;
                break;
            case EnumOperator.EDIT:
                dr["UpdateTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                dr["UpdateUser"] = SessionManager.UserID;
                break;
            default:
                break;
        }
    }
    public static int RemoveDataRow(string tableName, string key, string value)
    {
        return BaseAccess.ExecuteNonQuery(ConfigManager.ConnectionString, string.Format("delete from {0} where {1} ='{2}'", tableName, key, value));
    }
    /// <summary>
    /// 获取各类单的编号:入采购单，入库单，领料单，出库单，
    /// </summary>
    /// <param name="kind"></param>
    /// <returns></returns>
    public static string GetCode(EnumCodeKind kind)
    {
        string str = string.Empty;
        switch (kind)
        {
            case EnumCodeKind.CaiGou:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            case EnumCodeKind.RuKu:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            case EnumCodeKind.LingLiao:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            case EnumCodeKind.ChuKu:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            case EnumCodeKind.PanDian:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            case EnumCodeKind.HuiShou:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            default:
                str = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
        }
        return str;
    }
}
//各类单的枚举类
public enum EnumCodeKind
{
    CaiGou = 1,
    RuKu = 2,
    LingLiao = 3,
    ChuKu = 4,
    PanDian = 5,
    HuiShou = 6
}

public enum EnumOperator
{
    DEFAULT=0,
    NULL=1,
    ADD = 2,
    EDIT = 3,
    DELETE = 4,
    QUERY = 5,
    EXPORT = 6,
    UNDO = 7
}

public class GoodsCategoryAccess : SqlTree
{
    public const string TABLE_NAME = "IC_GoodsCategory";

    public GoodsCategoryAccess(SqlConnection conn)
        : base(conn)
    {

    }

    protected override void InitTableName()
    {
        this.TableName = TABLE_NAME;
    }


    protected override void InitCommandParameters(ref SqlParameterCollection pc)
    {
        pc.Add(new SqlParameter("@Description", SqlDbType.VarChar, 254));
        pc["@Description"].SourceColumn = "Description";

        pc.Add(new SqlParameter("@Published", SqlDbType.Bit));
        pc["@Published"].SourceColumn = "Published";

        pc.Add(new SqlParameter("@SubCount", SqlDbType.Int));
        pc["@SubCount"].SourceColumn = "SubCount";

        pc.Add(new SqlParameter("@StoreLocationID", SqlDbType.VarChar, 36));
        pc["@StoreLocationID"].SourceColumn = "StoreLocationID";
    }

    public virtual string GetStoreLocationID(string nodeID)
    {
        DataSet ds = GetByNodeID(nodeID, true);
        return ds.Tables[0].Rows[0]["StoreLocationID"].ToString();
    }
}


public class GoodsImageAccess
{
    public static DataSet GetEmptyDataSet()
    {
        return BaseAccess.GetDataSet(ConfigManager.ConnectionString,
                "select * from IC_GoodsImage where 1=2", "IC_GoodsImage");
    }

    public static DataSet GetDataSet(string productID)
    {
        return BaseAccess.GetDataSet(ConfigManager.ConnectionString,
                "select * from IC_GoodsImage where GoodsID = '" + productID + "'", "IC_GoodsImage");
    }

    public static DataSet GetDataSet(string productID, string Category)
    {
        return BaseAccess.GetDataSet(ConfigManager.ConnectionString,
                "select * from IC_GoodsImage where GoodsID = '" + productID +
                "' and Category='" + Category + "'", "IC_GoodsImage");
    }

    public static string GetLargeImageFileName(string productID)
    {
        return BaseAccess.ExecuteScalar(ConfigManager.ConnectionString,
            "select LargeImageFileName from IC_GoodsImage where Category='index' and GoodsID='" + productID + "'");
    }

    public static string GetThumbImageFileName(string productID)
    {
        return BaseAccess.ExecuteScalar(ConfigManager.ConnectionString,
            "select ThumbImageFileName from IC_GoodsImage where Category='index' and GoodsID='" + productID + "'");
    }
    
}

/// <summary>
/// 确定选择物料后事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void GoodItemSelectEventHandler(object sender, GoodItemSelectEventArgs e);
public class GoodItemSelectEventArgs : EventArgs
{
    public DataSet SelectDataSet { get; set; }
    public DataRow[] SelectedRow { get; set; }
}
/// <summary>
/// 选择人员后执行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void UserSelectEventHandler(object sender, UserSelectEventArgs e);
public class UserSelectEventArgs : EventArgs
{
    public DataSet SelectDataSet { get; set; }
}



/// <summary>
/// 代码类别配置
/// </summary>
public class PMSCodeCategory
{

    public static string UserWorker = "0";
    public static string UserReceiver = "1";

    /// <summary>
    ///PMS_物料仓库
    /// </summary>
    public static string PMS_GoodStorage
    {
        get
        {
            return "PMS_物料仓库";
        }
    }
    /// <summary>
    /// PMS_用户意见
    /// </summary>
    public static string PMS_CustomerSuggest
    {
        get
        {
            return "PMS_用户意见";
        }
    }
    /// <summary>
    /// 任务类型
    /// </summary>
    public static string PMS_TASKTYPE
    {
        get
        {
            return "PMS_任务类型";
        }
    }
    /// <summary>
    /// 照明灯类型
    /// </summary>
    public static string PMS_ZMDLX
    {
        get
        {
            return "PMS_照明灯类型";
        }
    }
    /// <summary>
    /// 部门组别
    /// </summary>
    public static string PMS_BMZB
    {
        get
        {
            return "PMS_部门组别";
        }
    }
    /// <summary>
    /// 对外合同
    /// </summary>
    public static string PMS_DWHT
    {
        get
        {
            return "PMS_对外合同";
        }
    }
    /// <summary>
    /// PMS_会议室
    /// </summary>
    public static string PMS_MEETAD
    {
        get
        {
            return "PMS_会议室";
        }
    }
    /// <summary>
    /// PMS_工时类型
    /// </summary>
    public static string PMS_WORKTYPE
    {
        get
        {
            return "PMS_工时类型";
        }
    }

    /// <summary>
    /// PMS_设备保养分类
    /// </summary>
    public static string PMS_EQUIPMENT
    {
        get
        {
            return "PMS_设备保养分类";
        }
    }
    

    #region 采购类型
    /// <summary>
    /// 采购类型
    /// </summary>
    public static string PMS_CGLX
    {
        get
        {
            return "PMS_采购类型";
        }
    }

    /// <summary>
    /// PMS_采购单来源
    /// </summary>
    public static string PMS_CGSOURCE
    {
        get
        {
            return "PMS_采购单来源";
        }
    }
    /// <summary>
    /// 采购类型-材料采购
    /// </summary>
    public static string PMS_CGLX_CaiGou
    {
        get
        {
            return "001";
        }
    }

    /// <summary>
    /// 采购类型-工具采购
    /// </summary>
    public static string PMS_CGLX_GongJu
    {
        get
        {
            return "002";
        }
    }

    /// <summary>
    /// 采购类型办-办公用品采购
    /// </summary>
    public static string PMS_CGLX_BanGongYongPin
    {
        get
        {
            return "003";
        }

    } 

    /// <summary>
    /// 采购类型-其他采购
    /// </summary>
    public static string PMS_CGLX_QiTa
    {
        get
        {
            return "004";
        } 
    }
    public static string GetCGLXName(string value)
    {
        switch (value)
        {
            case "001":
                return "材料采购";
            case "002":
                return "工具采购";
            case "003":
                return "办公用品采购";
            case "004": 
                return "其他采购";
            default:
                return "";
        }
    }
    #endregion

    #region 入库类型
    /// <summary>
    /// 入库类型
    /// </summary>
    public static string PMS_RKLX
    {
        get
        {
            return "PMS_入库类型";
        }
    }
    /// <summary>
    /// 入库类型-采购入库
    /// </summary>
    public static string PMS_RKLX_CaiGou
    {
        get
        {
            return "001";
        }  
    } 

    /// <summary>
    /// 入库类型-领料入库
    /// </summary>
    public static string PMS_RKLX_LingLiao
    {
        get
        {
            return "002";
        }   
    } 

    /// <summary>
    /// 入库类型-借还入库
    /// </summary>
    public static string PMS_RKLX_JieHuan
    { 
        get
        {
            return "003";
        }   
    } 

    /// <summary>
    /// 入库类型-其它入库
    /// </summary>
    public static string PMS_RKLX_QiTa
    {
        get
        {
            return "004";
        }
    }
    #endregion

    #region 出库业务
    /// <summary>
    /// 出库类型
    /// </summary>
    public static string PMS_CKLX
    {
        get
        {
            return "PMS_出库类型";
        }
    }

    /// <summary>
    /// 出库类型-借用出库
    /// </summary>
    public static string PMS_CKLX_JieHuan
    {
        get
        {
            return "001";
        }
    } 
     
    /// <summary>
    /// 出库类型-领料出库
    /// </summary>
    public static string PMS_CKLX_LingLiao
    {
        get
        {
            return "002";
        }

    }  
    /// <summary>
    /// 出库类型-其它入库
    /// </summary>
    public static string PMS_CKLX_QiTa
    {
        get
        {
            return "003";
        }
    } 

    #endregion 

    #region  工程项目

    /// <summary>
    /// 工程项目投资类别
    /// </summary>
    public static string PMS_P_TZLB
    {
        get
        {
            return "PMS_投资类别";
        }
    }
    /// <summary>
    /// 工程项目投资类别
    /// </summary>
    public static string PMS_P_ZT
    {
        get
        {
            return "PMS_项目状态";
        }
    }
    /// <summary>
    /// 工程项目状态-立项
    /// </summary>
    public static string PMS_P_LiXiang
    {
        get
        {
            return "01";
        }
    }
    /// <summary>
    /// 工程项目状态-实施
    /// </summary>
    public static string PMS_P_ShiShi
    {
        get
        {
            return "02";
        }
    }
    /// <summary>
    /// 工程项目状态-结算
    /// </summary>
    public static string PMS_P_JieSuan
    {
        get
        {
            return "03";
        }
    }
    /// <summary>
    /// 工程项目状态-作废
    /// </summary>
    public static string PMS_P_ZuoFei
    {
        get
        {
            return "04";
        }
    }
    public static string GetProjectStateName(string value)
    {
        string result = string.Empty;
        switch (value)
        {
            case "01":
                result = "立项";
                break;
            case "02":
                result = "实施";
                break;
            case "03":
                result = "结算";
                break;
            case "04":
                result = "作废";
                break;
        }
        return result;
    }
    #endregion

    /// <summary>
    /// 人员状态
    /// </summary>
    public static string PMS_RYZT
    {
        get
        {
            return "PMS_人员状态";
        }
    }

    /// <summary>
    /// 人员属性
    /// </summary>
    public static string PMS_RYSX
    {
        get
        {
            return "PMS_人员属性";
        }
    }
    /// <summary>
    /// 客户类型
    /// </summary>
    public static string PMS_KHLX
    {
        get
        {
            return "PMS_客户类型";
        }
    }
    /// <summary>
    /// PMS_任务项目_零修
    /// </summary>
    public static string PMS_TASKLX
    {
        get
        {
            return "PMS_任务项目_零修";
        }
    }
    /// <summary>
    /// 日常零修业务类型
    /// </summary>
    public static string PMS_RCLX
    {
        get
        {
            return "1";
        }
    }
    /// <summary>
    /// 保养业务类型
    /// </summary>
    public static string PMS_BY
    {
        get
        {
            return "2";
        }
    }
    /// <summary>
    /// 大中修业务类型
    /// </summary>
    public static string PMS_DZX
    {
        get
        {
            return "3";
        }
    }
    /// <summary>
    /// 工程业务类型
    /// </summary>
    public static string PMS_GC
    {
        get
        {
            return "4";
        }
    }
    public static string GetName(string value)
    { 
        string result = string.Empty;
        switch (value)
        {
            case "1":
                result = "零修";
                break;
            case "2":
                result = "保养";
                break;
            case "3":
                result = "大中修";
                break;
            case "4":
                result = "工程";
                break;
        }
        return result;
    }
}
/// <summary>
/// PMS常量类
/// </summary>
public class PMSConst
{
    //start存SessionManager用
    //表名
    public const string m_TableItemName = "m_subtable";
    //表主键
    public const string m_Key = "m_key";
    //关联主键
    public const string m_SubKey = "m_subkey";
    //关联值
    public const string m_SubValue = "m_subvalue";
    //end存SessionManager用 

    public static string CurrentPath = AppDomain.CurrentDomain.BaseDirectory;
    public static string UserPhotoPath
    {
        get
        {
            string path = CurrentPath + "\\PMSWeb\\Photo\\Person\\";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            return "~\\PMSWeb\\Photo\\Person\\";
        }
    }

    public static string GoodPhotoPath
    { 
        get
        {
            string path = CurrentPath + "\\PMSWeb\\Photo\\Goods\\";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            return "~\\PMSWeb\\Photo\\Goods\\";
        }
    }
    public static string PMSDispatchDocTemplatePath = CurrentPath + "PMSWeb\\doc\\PMSTaskDispatch.doc";
    public static string PMSWarningTemplatePath = CurrentPath + "PMSWeb\\doc\\warning.xls"; 
    public static string PMSMeetInfoTemplatePath = CurrentPath + "PMSWeb\\doc\\MeetInfo.xls";
    public static string PMSPurchaseInfoTemplatePath = CurrentPath + "PMSWeb\\doc\\PurchaseInfo.xls";
    public static string PMSTaskTrackingListTemplatePath = CurrentPath + "PMSWeb\\doc\\TaskTrackingList.xls";
    public static string PMSTempPath
    {
        get
        {
            string path = CurrentPath+"PMSWeb\\doc\\temp\\";
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            return path;
        }
    }
    public static string PMSPersonWorkloadTemplatePath = CurrentPath + "PMSWeb\\doc\\PersonWorkload.xls";
}


#region 采购单、入库单、出库单、领料单、盘点单状态

public class OrderStatus
{
    /// <summary>
    /// 待处理
    /// </summary>
    public static int WaitProcess
    {
        get
        {
            return 0;
        }
    }
    /// <summary>
    /// 已入库
    /// </summary>
    public static int IsStockIn
    {
        get
        {
            return 1;
        }
    }
    /// <summary>
    /// 已审核
    /// </summary>
    public static int IsAudited
    {
        get
        {
            return 2;
        }
    }
    public static string GetName(string value)
    {
        string result = string.Empty;
        if ("0".Equals(value))
        {
            //待处理 
            result = "待处理";
        }
        else if ("1".Equals(value))
        { 
            result = "已入库";
        }
        else if ("2".Equals(value))
        {
            //已审核
            result = "已审核";
        } 
        return result;
    }
}

public class StockOutStatus
{
    public static int UnBack
    {
        get
        {
            return 0;
        }
    }
    /// <summary>
    ///已归还
    /// </summary>
    public static int IsBack
    {
        get
        {
            return 1;
        }
    } 
    public static string GetName(string value)
    {
        string result = string.Empty;
        if ("0".Equals(value))
        { 
            result = "未归还";
        }
        else if ("1".Equals(value))
        {
            result = "已归还";
        }
        return result;
    }
}
public class StockInStatus
{
    /// <summary>
    /// 待处理（未入库）
    /// </summary>
    public static int UnStockIn
    {
        get
        {
            return 0;
        }
    }
    /// <summary>
    /// 已入库
    /// </summary>
    public static int IsStockIn
    {
        get
        {
            return 1;
        }
    }
    public static string GetName(string value)
    {
        string result = string.Empty;
        if ("0".Equals(value))
        {
            //待处理 
            result = "待处理";
        }
        else if ("1".Equals(value))
        {
            //已入库
            result = "已入库";
        }
        return result;
    }
}

#region 人员是否工作状态

public class UserStatus
{
    public static int Waiting
    {
        get
        {
            return 0;
        }
    }
    public static int Working
    {
        get
        {
            return 1;
        }
    }
    public static string GetName(string value)
    {
        string result = string.Empty;
        if ("0".Equals(value))
        { 
            result = "空闲";
        }
        else
        { 
            result = "工作中";
        }
        return result;
    }
}
#endregion


/// <summary>
/// 维修保养任务状态
/// </summary>
public class PMSTaskState
{
    /// <summary>
    /// 已受理
    /// </summary>
    public static int Running
    {
        get
        {
            return 0;
        }
    }
    /// <summary>
    /// 已派发
    /// </summary>
    public static int Dispatch
    {
        get
        {
            return 1;
        }
    }
    /// <summary>
    /// 已完成
    /// </summary>
    public static int Complete
    {
        get
        {
            return 2;
        }
    }  
    /// <summary>
    /// 已作废
    /// </summary>
    public static int IsRemove
    {
        get
        {
            return 3;
        }
    }
 
    public static string GetName(string value)
    {
        string result = string.Empty;
        if ("0".Equals(value))
        {
            result = "已受理";
        }
        else if ("1".Equals(value))
        {
            result = "已派发";
        }
        else if ("2".Equals(value))
        {
            result = "已完成";
        }
        else if ("3".Equals(value))
        {
            result = "已作废";
        }
        return result;
    }
}


/// <summary>
/// 维修物料状态
/// </summary>
public class PMSGoodsState
{
    /// <summary>
    /// 0没有涉及到物料
    /// </summary>
    public static int None
    {
        get
        {
            return 0;
        }
    }

    /// <summary>
    /// 未出库
    /// </summary>
    public static int NotOut
    {
        get
        {
            return 1;
        }
    }
    /// <summary>
    /// 已出库
    /// </summary>
    public static int IsOut
    {
        get
        {
            return 2;
        }
    }   
    public static string GetName(string value)
    {
        string result = string.Empty;
        if ("0".Equals(value))
        {
            //受理时更新主表
            //库房人员显示预计出库单时，需要过滤此状态
            result = "";
        }
        else if ("1".Equals(value))
        {
            //物料配置完成时更新主表
            result = "未出库";
        }
        else if ("2".Equals(value))
        {
            //库房人查看时更新主表
            result = "已出库";
        }
        return result;
    }
}


/// <summary>
/// 维修任务来源状态
/// </summary>
public class PMSSourceState
{
    /// <summary>
    /// 123报修
    /// </summary>
    public static int From123
    {
        get
        {
            return 0;
        }
    }
    /// <summary>
    /// 自填数据
    /// </summary>
    public static int FromUs
    {
        get
        {
            return 1;
        }
    }
}


#endregion



#region  文件导出帮助类
public class DocumentHelper
{
    private static DocumentHelper m_Instance;

    public static DocumentHelper Instance
    {
        get 
        {
            if (m_Instance == null)
            {
                m_Instance = new DocumentHelper();
            }
            return DocumentHelper.m_Instance; 
        }
    }

    private  string cmdCodeText = "select MAX(a.SNDispatch) as SNDispatch from PMS_TaskItem a where 1=1 and a.SNYear='{0}' and a.SNGroup='{1}' and a.SNTask='{2}' and a.SNProject='{3}'";
    public  string GetDispatchCode(string SNYear,string SNGroup,string SNTask,string SNProject)
    {
        string str = string.Format(cmdCodeText, new object[] { SNYear, SNGroup, SNTask, SNProject });
        DataSet ds = BaseAccess.GetDataSet(ConfigManager.ConnectionString, str, "PMS_TaskItem");
        if (ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            if (string.IsNullOrEmpty(row["SNDispatch"].ToString()))
                return "0000";
            int i = Convert.ToInt32(row["SNDispatch"].ToString());
            str = Convert.ToString(i+1).PadLeft(4,'0');
            return str;
        }
        else
        {
            return "0000";
        }
    }
    #region 导出物料预警结果
    public static string ExportWarning(string templatePath, string filePath,DataSet ds)
    {
        string result = string.Empty;
        IExcel excel = null;
        try
        {
            if (System.IO.File.Exists(templatePath))
            {
                System.IO.File.Copy(templatePath, filePath);
                excel = OfficeFactory.GetExcelObject();
                excel.Open(filePath, false, false);
                //excel.SaveAS(filePath);
                //excel.SetSheetIndex(0);
                excel.SetSheetName("库存预警结果");
                WriteExcelRow(excel, ds);
                excel.Save();
            }
            else
            {
                result = "库存预警Excel模版文件不存在";
            }
        }
        catch (Exception ex)
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
            result = "导出预警结果出现异常：" + ex.ToString();
        }
        finally
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
        }
        return result;
    }

    private static void WriteExcelRow( IExcel excel,DataSet ds)
    {
        if (ds == null || ds.Tables.Count == 0) return;
        int startRow = 2;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            excel.SetCellValue(startRow, 1, startRow-1);
            excel.SetCellValue(startRow, 2, dr["GoodsCode"]);
            excel.SetCellValue(startRow, 3, dr["Subject"]);
            excel.SetCellValue(startRow, 4, dr["CategoryName"]);
            excel.SetCellValue(startRow, 5, dr["GoodsMaker"]);
            excel.SetCellValue(startRow, 6, dr["TypeModel"]);
            excel.SetCellValue(startRow, 7, dr["GoodsUnit"]);
            excel.SetCellValue(startRow, 8, dr["GoodsDownCount"]);
            excel.SetCellValue(startRow, 9, dr["ActiveCount"]);
            startRow++;
        }
    }
    #endregion

    #region 导出工作量统计

    public static string ExportPersonWorkloadAll(string templatePath, string filePath, DataSet ds)
    {
        string result = string.Empty;
        IExcel excel = null;
        try
        {
            if (System.IO.File.Exists(templatePath))
            {
                System.IO.File.Copy(templatePath, filePath);
                excel = OfficeFactory.GetExcelObject();
                excel.Open(filePath, false, false);
                excel.SetCellValue(1, 1, DateTime.Now.Year + "年工作量统计表");
                WriteExcelPersonWorkloadRow(excel, ds.Tables[0].Select(""));
                excel.Save();
            }
            else
            {
                result = "个人工作量统计Excel模版文件不存在";
            }
        }
        catch (Exception ex)
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
            result = "导出个人工作量统计结果出现异常：" + ex.ToString();
        }
        finally
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
        }
        return result;
    }
    public static string ExportPersonWorkloadOne(string templatePath, string filePath, DataSet ds,string monthstr)
    {
        string result = string.Empty;
        IExcel excel = null;
        try
        {
            if (System.IO.File.Exists(templatePath))
            {
                System.IO.File.Copy(templatePath, filePath);
                excel = OfficeFactory.GetExcelObject();
                excel.Open(filePath, false, false);
                excel.SetCellValue(1, 1, monthstr + "月份工作量统计表");
                WriteExcelPersonWorkloadRow(excel, ds.Tables[0].Select(""));
                excel.Save();
            }
            else
            {
                result = "个人工作量统计Excel模版文件不存在";
            }
        }
        catch (Exception ex)
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
            result = "导出个人工作量统计结果出现异常：" + ex.ToString();
        }
        finally
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
        }
        return result;
    }
    private static void WriteExcelPersonWorkloadRow(IExcel excel, DataRow[] rows)
    {
        if (rows == null || rows.Length == 0) return;
        int startRow = 3;
        foreach (DataRow dr in rows)
        {
            excel.SetCellValue(startRow, 1, startRow - 2);
            excel.SetCellValue(startRow, 2, dr["UserName"]);
            excel.SetCellValue(startRow, 3, dr["TaskCount"]);
            excel.SetCellValue(startRow, 4, dr["WorkTime"]);
            excel.SetCellValue(startRow, 5, dr["WorkValue"]);
            excel.SetCellValue(startRow, 6, dr["GroupName"]);
            startRow++;
        }
    }
    #endregion 

    #region 导出派工单
    /// <summary>
    /// 根据任务导出所有派工单
    /// </summary>
    /// <param name="taskID"></param>
    /// <returns></returns>
    public static string ExportAllDispatchDocument(string templatePath, string filePath, string taskID)
    {
        string result = string.Empty;
        string cmdText = @"select a.TaskItemID,a.TaskID,a.SNYear,a.SNGroup,a.SNTask,a.SNProject,E.CodeName as SNProjectName,a.SNDispatch,a.TaskState,D.CodeName as GroupName,C.Guest,C.Phone,C.Location,C.RoomNo,C.HouseNo,C.CreateDate,C.TroubleDescription,case when B.status is null then '" + PMSGoodsState.NotOut + @"' else B.status end ReqStatus, (select Username from T_User where UserID=C.UserID) Username from PMS_TaskItem a 
                   left join IC_Requisition B on B.TaskID=a.TaskID left join PMS_TaskLX C on c.TaskID = a.TaskID left join T_CodeConfig D on D.CodeValue=a.SNGroup AND D.Category = '" + PMSCodeCategory.PMS_BMZB + "' left join T_CodeConfig E on E.CodeValue=a.SNProject AND E.Category = '" + PMSCodeCategory.PMS_TASKLX + "'  where 1=1  and a.TaskID='" + taskID + "'";
        //受理基本信息
        DataSet ds = BaseAccess.GetDataSet(ConfigManager.ConnectionString, cmdText, "PMS_TaskItem");
        //人员派发信息
        cmdText = @"SELECT T.*,A.Username,A.GroupCode,A.Mobile,B.OrgID,D.Subject as OrgName,C.CodeName as GroupName  FROM PMS_TaskUser T inner join PMS_User A on A.UserID = T.UserID 
                    left join T_OrgUser B on B.UserID = T.UserID 
                    left join  T_CodeConfig C on C.CodeValue=A.GroupCode  AND C.Category = '" + PMSCodeCategory.PMS_BMZB + @"' 
                    left join T_OrgTree D on D.NodeID=B.OrgID where 1=1 and T.UserType='" + PMSCodeCategory .UserWorker+ "' and T.TaskID in (select TaskItemID from PMS_TaskItem where  TaskID='" + taskID + "') ";
        BaseAccess.FillDataSet(ConfigManager.ConnectionString, ds, cmdText, "PMS_TaskUser");
        //配料信息
        cmdText = "SELECT T.*,B.TaskID,A.SUBJECT AS CategoryName FROM IC_RequisitionItem T LEFT JOIN " + GoodsCategoryAccess.TABLE_NAME + " A ON A.NODEID=T.CategoryID LEFT JOIN IC_Requisition B ON B.RequisitionID=T.RequisitionID where 1=1 and T.RequisitionID in (select RequisitionID from IC_Requisition where TaskID in (select TaskItemID from PMS_TaskItem where  TaskID='" + taskID + "'))";

        BaseAccess.FillDataSet(ConfigManager.ConnectionString, ds, cmdText, "IC_RequisitionItem");
        if (ds.Tables["PMS_TaskItem"].Rows.Count == 0)
        {
            return "没有派发人员信息，请确定是否已经保存！";
        }
        IWord word = null;
        try
        {
            if (System.IO.File.Exists(templatePath))
            {
                word = OfficeFactory.GetWordObject();
                word.Open(templatePath, true, false);
                word.SaveAs(filePath);
                WirteContent(word, ds);
                word.MoveEnd();
                word.Style.ClearFormatting();
                if (word != null)
                {
                    word.Save();
                    word.Quit();
                    word = null;
                }
            }
            else
            {
                result = "模版文件不存在！";
            }
        }
        catch (Exception ex)
        {
            if (word != null)
            {
                word.Quit();
                word = null;
            }
            result = "导出派工单出现异常：" + ex.ToString();
        }
        finally
        {
            if (word != null)
            {
                word.Quit();
                word = null;
            }
        }
        return result;
    }
    /// <summary>
    /// 根据任务导出派工单
    /// </summary>
    /// <param name="taskID"></param>
    /// <returns></returns>
    public static string ExportDispatchDocument(string templatePath,string filePath,string taskID)
    {
        string result = string.Empty;
        string cmdText = @"select a.TaskItemID,a.TaskID,a.SNYear,a.SNGroup,a.SNTask,a.SNProject,E.CodeName as SNProjectName,a.SNDispatch,a.TaskState,D.CodeName as GroupName,C.Guest,C.Phone,C.Location,C.RoomNo,C.HouseNo,C.CreateDate,C.TroubleDescription,case when B.status is null then '" + PMSGoodsState.NotOut + @"' else B.status end ReqStatus, (select Username from T_User where UserID=C.UserID) Username from PMS_TaskItem a 
                   left join IC_Requisition B on B.TaskID=a.TaskID left join PMS_TaskLX C on c.TaskID = a.TaskID left join T_CodeConfig D on D.CodeValue=a.SNGroup AND D.Category = '" + PMSCodeCategory.PMS_BMZB + "' left join T_CodeConfig E on E.CodeValue=a.SNProject AND E.Category = '" + PMSCodeCategory.PMS_TASKLX + "'  where 1=1  and a.TaskItemID='" + taskID + "'";
        //受理基本信息
        DataSet ds = BaseAccess.GetDataSet(ConfigManager.ConnectionString, cmdText, "PMS_TaskItem");
        //人员派发信息
        cmdText = @"SELECT T.*,A.Username,A.GroupCode,A.Mobile,B.OrgID,D.Subject as OrgName,C.CodeName as GroupName  FROM PMS_TaskUser T inner join PMS_User A on A.UserID = T.UserID 
                    left join T_OrgUser B on B.UserID = T.UserID 
                    left join  T_CodeConfig C on C.CodeValue=A.GroupCode  AND C.Category = '" + PMSCodeCategory.PMS_BMZB + @"' 
                    left join T_OrgTree D on D.NodeID=B.OrgID where 1=1 and T.UserType='" + PMSCodeCategory.UserWorker + "' and T.TaskID='" + taskID + "'";
        BaseAccess.FillDataSet(ConfigManager.ConnectionString, ds, cmdText, "PMS_TaskUser");
        //配料信息
        cmdText = "SELECT T.*,B.TaskID,A.SUBJECT AS CategoryName FROM IC_RequisitionItem T LEFT JOIN " + GoodsCategoryAccess.TABLE_NAME + " A ON A.NODEID=T.CategoryID LEFT JOIN IC_Requisition B ON B.RequisitionID=T.RequisitionID where 1=1 and T.RequisitionID in (select RequisitionID from IC_Requisition where TaskID='" + taskID + "')";

        BaseAccess.FillDataSet(ConfigManager.ConnectionString, ds, cmdText, "IC_RequisitionItem");
        if (ds.Tables["PMS_TaskItem"].Rows.Count == 0)
        {
            return "没有派发人员信息，请确定是否已经保存！"; 
        }
        IWord word = null;
        try
        {
            if (System.IO.File.Exists(templatePath))
            {
                word = OfficeFactory.GetWordObject();
                word.Open(templatePath, true, false);
                word.SaveAs(filePath);
                WirteContent(word, ds);
                word.MoveEnd();
                word.Style.ClearFormatting();
                if (word != null)
                {
                    word.Save();
                    word.Quit();
                    word = null;
                }
            }
            else
            {
                result = "模版文件不存在！";
            }
        }
        catch (Exception ex)
        {
            if (word != null)
            { 
                word.Quit();
                word = null;
            }
            result = "导出派工单出现异常："+ex.ToString();
        }
        finally
        {
            if (word != null)
            { 
                word.Quit();
                word = null;
            }
        }
        return result;
    }
    private static void WirteContent(IWord word, DataSet ds)
    {
        try
        {
            word.WholeStory();
            word.Cut();
            DataRow[] drs = null;
            foreach (DataRow dr in ds.Tables["PMS_TaskItem"].Rows)
            {
                string code = dr["SNYear"].ToString() + "-" + dr["SNGroup"].ToString() + "-" + dr["SNTask"].ToString() + "-" + dr["SNProject"].ToString() + "-" + dr["SNDispatch"].ToString();
                string discode = dr["SNYear"].ToString() + dr["SNGroup"].ToString() + dr["SNTask"].ToString() +  dr["SNProject"].ToString() + dr["SNDispatch"].ToString();
                try
                {
                    string fileName = HttpContext.Current.Server.MapPath(SessionManager.TempVirtualFolder) + Guid.NewGuid().ToString() + ".bmp";
                    System.Drawing.Image image = Code128Rendering.MakeBarcodeImage(code, 2, true);
                    image.Save(fileName, ImageFormat.Bmp);
                    image.Dispose();
                    code = fileName;
                }
                catch (Exception ex)
                {
                    code = "产生条码错误：" + ex.Message;
                }
                word.Paste();

                word.Find("{编号}", 0, 0);
                word.SelectionText = "";
                word.InsertImage(code);

                word.Find("{部门类别}", 0, 0);
                word.SelectionText = dr["GroupName"].ToString();

                word.Find("{项目名称}", 0, 0);
                word.SelectionText = dr["SNProjectName"].ToString();
                word.Find("{任务号}", 0, 0);
                word.SelectionText = discode;
                word.Find("{受理人}", 0, 0);
                word.SelectionText = dr["Username"].ToString();
                word.Find("{受理日期}", 0, 0);
                //word.SelectionText = Convert.ToDateTime(dr["CreateDate"]).ToString("yyyy-MM-dd");
                word.SelectionText = DateTime.Now.ToString("yyyy-MM-dd");
                word.Find("{受理时间}", 0, 0);
                //word.SelectionText = Convert.ToDateTime(dr["CreateDate"]).ToString("HH:ss:mm");
                word.SelectionText = DateTime.Now.ToString("HH:mm:ss");
                word.Find("{单位用户}", 0, 0);
                word.SelectionText = dr["Guest"].ToString();
                word.Find("{楼号}", 0, 0);
                word.SelectionText = dr["HouseNo"].ToString();
                word.Find("{房间号}", 0, 0);
                word.SelectionText = dr["RoomNo"].ToString();
                word.Find("{联系电话}", 0, 0);
                word.SelectionText = dr["Phone"].ToString();
                word.Find("{事由}", 0, 0);
                word.SelectionText = dr["TroubleDescription"].ToString();
                word.Find("{处理情况}", 0, 0);
                word.SelectionText = "";
                word.Find("{到达时间}", 0, 0);
                word.SelectionText = "";
                word.Find("{完成时间}", 0, 0);
                word.SelectionText = "";

                //添加派遣人员
                int i = 1;
                drs = ds.Tables["PMS_TaskUser"].Select("TaskID='" + dr["TaskItemID"].ToString() + "'");
                foreach (DataRow _dr in drs)
                {
                    word.Find("{人" + i + "}", 0, 0);
                    word.SelectionText = _dr["Username"].ToString();
                    i++;
                    if (i > 8)
                        break;
                }
                for (int j = i - 1; j < 9; j++)
                {
                    word.Find("{人" + j + "}", 0, 0);
                    word.SelectionText = "";
                }

                //添加消耗材料 
                StringBuilder str = new StringBuilder();
                drs = ds.Tables["IC_RequisitionItem"].Select("TaskID='" + dr["TaskItemID"].ToString() + "'");
                foreach (DataRow _dr in drs)
                {
                    str.Append(_dr["SUBJECT"].ToString() + _dr["GoodsCount"].ToString() + _dr["GoodsUnit"].ToString() + ";");
                }
                word.Find("{消耗材料}", 0, 0);
                word.SelectionText = str.ToString();

                word.MoveEnd();
            }
        }
        catch(Exception ex)
        { 
            if (word != null)
            {
                word.Quit();
                word = null;
            }
            throw ex;
        }
    }

    #endregion

    #region 导出会议信息
    public static string ExportMeetInfo(string templatePath, string filePath,string MeetID)
    {
        string result = string.Empty;
        IExcel excel = null;
        try
        {
            DataSet ds = BaseAccess.GetDataSet(ConfigManager.ConnectionString, "select * from PMS_Meet where MeetID='" + MeetID + "'", "PMS_Meet");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (System.IO.File.Exists(templatePath))
                {
                    
                    System.IO.File.Copy(templatePath, filePath);
                    excel = OfficeFactory.GetExcelObject();
                    excel.Open(filePath, false, false);
                    //excel.SaveAS(filePath);
                    //excel.SetSheetIndex(0);
                    excel.SetSheetName("会议详细信息");

                    excel.SetCellValue(1,1, dr["Subject"]);

                    //开始写入
                    int startRow = 3; 
                    excel.SetCellValue(startRow, 2, dr["Subject"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Leader"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["StartTime"]);
                    excel.SetCellValue(startRow, 4, dr["EndTime"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["MeetAddress"]);
                    excel.SetCellValue(startRow, 4, dr["MeetOrg"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["ContactPerson"]);
                    excel.SetCellValue(startRow, 4, dr["ContactTel"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["ApplyPerson"]);
                    excel.SetCellValue(startRow, 4, dr["CheckPerson"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["GroupRePerson"]);
                    excel.SetCellValue(startRow, 4, dr["SecurityRePerson"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["PersonCount"]);

                    startRow = startRow + 2;
                    excel.SetCellValue(startRow, 2, dr["Microphone1"]);
                    excel.SetCellValue(startRow, 4, dr["Microphone2"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Recording"]);
                    excel.SetCellValue(startRow, 4, dr["Video"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Broadcast"]);
                    excel.SetCellValue(startRow, 4, dr["Lighting"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Ladder1"]);
                    excel.SetCellValue(startRow, 4, dr["Ladder2"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Music"]);
                    excel.SetCellValue(startRow, 4, dr["Projection"]); 
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Newsub"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Remark1"]); 
                     
                    startRow = startRow + 2;
                    excel.SetCellValue(startRow, 2, dr["Seat"]);
                    excel.SetCellValue(startRow, 4, dr["Cover"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Cup"]);
                    excel.SetCellValue(startRow, 4, dr["Tea"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Flower"]);
                    excel.SetCellValue(startRow, 4, dr["Waiter"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Lounge"]);
                    excel.SetCellValue(startRow, 4, dr["Banner"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Remark2"]);

                    //结束写入
                    excel.Save();
                }
                else
                {
                    result = "导出的会议信息Excel模版文件不存在";
                }
            }
            else
            {
                result = "您要导出的会议信息不存在";
            }
        }
        catch (Exception ex)
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
            result = "导出的会议信息出现异常：" + ex.ToString();
        }
        finally
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
        }
        return result;
    }
    #endregion

    #region 导出采购单信息
    public static string ExportPurchaseInfo(string templatePath, string filePath, string PurchaseOrderID)
    {
        string result = string.Empty;
        IExcel excel = null;
        try
        {
            DataSet ds = BaseAccess.GetDataSet(ConfigManager.ConnectionString, "select T.*,B.CodeName as PurchaseSourceName,A.CodeName AS PurchaseTypeName" + PMSAccess.AppendSql_UserOrg + " from IC_PurchaseOrder T left join " + CodeConfigAccess.TableName + " A on A.CodeValue=T.PurchaseType left join " + CodeConfigAccess.TableName + " B on B.CodeValue=T.PurchaseSource where PurchaseOrderID='" + PurchaseOrderID + "'", "IC_PurchaseOrder");
            BaseAccess.FillDataSet(ConfigManager.ConnectionString, ds, "select * from IC_PurchaseOrderItem where PurchaseOrderId='" + PurchaseOrderID + "'", "IC_PurchaseOrderItem");

            if (ds.Tables["IC_PurchaseOrder"].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (System.IO.File.Exists(templatePath))
                {

                    System.IO.File.Copy(templatePath, filePath);
                    excel = OfficeFactory.GetExcelObject();
                    excel.Open(filePath, false, false);
                    //excel.SaveAS(filePath);
                    //excel.SetSheetIndex(0);
                    excel.SetSheetName("采购单信息"); 

                    //开始写入
                    int startRow = 2;
                    excel.SetCellValue(startRow, 2, dr["PurchaseDate"]);
                    excel.SetCellValue(startRow, 4, dr["PurchaseOrderCode"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["PurchaseHopeDate"]);
                    excel.SetCellValue(startRow, 4, dr["PurchaseTypeName"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["SupplierID"]);
                    excel.SetCellValue(startRow, 4, dr["PurchaseSourceName"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["TaskNo"]);
                    excel.SetCellValue(startRow, 4, dr["UserName"]);
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["OrgName"]); 
                    startRow++;
                    excel.SetCellValue(startRow, 2, dr["Description"]);  

                    startRow = startRow + 3;
                    int i = 1;
                    foreach (DataRow row in ds.Tables["IC_PurchaseOrderItem"].Rows)
                    {
                        excel.SetCellValue(startRow, 1,""+i);
                        excel.SetCellValue(startRow, 2, row["GoodsCode"]);
                        excel.SetCellValue(startRow, 3, row["Subject"]);
                        excel.SetCellValue(startRow, 4, row["TypeModel"]);
                        excel.SetCellValue(startRow, 5, row["GoodsCount"]);
                        excel.SetCellValue(startRow, 6, row["GoodsUnit"]);
                        excel.SetCellValue(startRow, 7, row["Description"]); 
                        startRow++;
                        i++;
                    } 
                    //结束写入
                    excel.Save();
                }
                else
                {
                    result = "导出的采购单信息Excel模版文件不存在";
                }
            }
            else
            {
                result = "您要导出的采购单信息不存在";
            }
        }
        catch (Exception ex)
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
            result = "导出的采购单信息出现异常：" + ex.ToString();
        }
        finally
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
        }
        return result;
    }
    #endregion

    #region 导出任务换还
    public static string ExportTaskList(string templatePath, string filePath, DataSet ds)
    {
        string result = string.Empty;
        IExcel excel = null;
        try
        {
            if (ds == null || ds.Tables.Count == 0) return "导出的任务单返还结果为空";

            if (System.IO.File.Exists(templatePath))
            {
                System.IO.File.Copy(templatePath, filePath);
                excel = OfficeFactory.GetExcelObject();
                excel.Open(filePath, false, false);
                excel.SetSheetName("任务单返还");
                int startRow = 2;
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    excel.SetCellValue(startRow, 1, startRow - 1);
                    excel.SetCellValue(startRow, 2, dr["TaskCode"]);
                    excel.SetCellValue(startRow, 3, dr["SNTask"]);
                    excel.SetCellValue(startRow, 4, dr["GroupName"]);
                    excel.SetCellValue(startRow, 5, dr["Guest"]);
                    excel.SetCellValue(startRow, 6, dr["HouseNo"]);
                    excel.SetCellValue(startRow, 7, dr["RoomNo"]);
                    excel.SetCellValue(startRow, 8, dr["Phone"]);
                    excel.SetCellValue(startRow, 9, dr["Username"]);
                    excel.SetCellValue(startRow, 10, Convert.ToDateTime(dr["CreateDate"]).ToString("yyyy-MM-dd HH:mm"));
                    excel.SetCellValue(startRow, 11, PMSTaskState.GetName(dr["TaskState"].ToString()));
                    excel.SetCellValue(startRow, 12, dr["ReqStatus"]);
                    startRow++;
                }
                excel.Save();
            }
            else
            {
                result = "导出的任务单返还Excel模版文件不存在";
            } 
        }
        catch (Exception ex)
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
            result = "导出的任务单返还出现异常：" + ex.ToString();
        }
        finally
        {
            if (excel != null)
            {
                excel.Close();
                excel = null;
            }
        }
        return result;
    }
    #endregion
}

#endregion


#region 代码帮助  暂时不用

public class CodeConst
{
    private static readonly string CurrentPath = AppDomain.CurrentDomain.BaseDirectory;

    private static readonly string FileName = CurrentPath + "CodeConfig.xml";

    private static CodeConst m_Instance;

    public static CodeConst Instance
    {
        get
        {
            if (m_Instance == null)
                m_Instance = new CodeConst();
            return CodeConst.m_Instance;
        }
        set { CodeConst.m_Instance = value; }
    }
    private NameValueCollection m_CategoryNameValue;

    public NameValueCollection CategoryNameValue
    {
        get { return m_CategoryNameValue; }
        set { m_CategoryNameValue = value; }
    }
    public CodeConst()
    {
        if (m_CategoryNameValue == null || m_CategoryNameValue.Count == 0)
        {
            m_CategoryNameValue = new NameValueCollection();
            XmlDocument doc = new XmlDocument();
            doc.Load(FileName);
            XmlNodeList xnl = doc.SelectNodes("/CodeList/Category/Item");
            foreach (XmlNode xn in xnl)
            {
                m_CategoryNameValue.Add(xn.Attributes["key"].Value, xn.Attributes["value"].Value);
            }
        }
    }
    public CodeConst(bool update)
    {
        if (update)
        {
            m_CategoryNameValue = new NameValueCollection();
            XmlDocument doc = new XmlDocument();
            doc.Load(FileName);
            XmlNodeList xnl = doc.SelectNodes("/CodeList/Category/Item");
            foreach (XmlNode xn in xnl)
            {
                m_CategoryNameValue.Add(xn.Attributes["key"].Value, xn.Attributes["value"].Value);
            }
            m_CodeList = PMSAccess.GetDataSet(ConfigManager.ConnectionString, "select * from T_CodeConfig order by SerialIndex").Tables[0];
            m_CategoryList = PMSAccess.GetDataSet(ConfigManager.ConnectionString, "select distinct Category from T_CodeConfig order by SerialIndex").Tables[0];
        }
    }
    private DataTable m_CategoryList;

    public DataTable CategoryList
    {
        get
        {
            if (m_CategoryList == null)
            {
                m_CategoryList = PMSAccess.GetDataSet(ConfigManager.ConnectionString, "select distinct Category from T_CodeConfig order by OrderIndex").Tables[0];
            }
            return m_CategoryList;
        }
    }

    private DataTable m_CodeList;

    public DataTable DataList
    {
        get
        {
            if (m_CodeList == null)
            {
                m_CodeList = PMSAccess.GetDataSet(ConfigManager.ConnectionString, "select * from T_CodeConfig order by OrderIndex").Tables[0];
            }
            return m_CodeList;
        }
    }
    public string GetCategoryCode(string value)
    {
        return CategoryNameValue[value];
    } 
    #region 代码类别配置 
    #endregion
}
#endregion


public class XingZhengAccess : SqlTree
{
    public const string TABLE_NAME = "PMS_XingzhengTree";

    public XingZhengAccess(SqlConnection conn)
        : base(conn)
    {

    }

    protected override void InitTableName()
    {
        this.TableName = TABLE_NAME;
    }


    protected override void InitCommandParameters(ref SqlParameterCollection pc)
    {
        pc.Add(new SqlParameter("@Description", SqlDbType.VarChar, 254));
        pc["@Description"].SourceColumn = "Description";

        pc.Add(new SqlParameter("@QueryCode", SqlDbType.VarChar, 50));
        pc["@QueryCode"].SourceColumn = "QueryCode";

        pc.Add(new SqlParameter("@StoreLocationID", SqlDbType.VarChar, 36));
        pc["@StoreLocationID"].SourceColumn = "StoreLocationID";
    }

    public virtual string GetStoreLocationID(string nodeID)
    {
        DataSet ds = GetByNodeID(nodeID, true);
        return ds.Tables[0].Rows[0]["StoreLocationID"].ToString();
    }
}
